#include "deepinappearance.h"

#include <QApplication>
#include <QFileInfo>
#include <QDebug>
#include <QDBusInterface>
#include <QDBusReply>
#include <QWindow>
#include <QScreen>

#ifdef COMPILE_ON_V23
#    define APPEARANCE_SERVICE "org.deepin.dde.Appearance1"
#    define APPEARANCE_PATH "/org/deepin/dde/Appearance1"
#else
#    define APPEARANCE_SERVICE "com.deepin.daemon.Appearance"
#    define APPEARANCE_PATH "/com/deepin/daemon/Appearance"
#endif


bool DeepinAppearance::setBackGround(const QString &pictureFilePath)
{
    if (QFileInfo::exists("/var/lib/deepin/permission-manager/wallpaper_locked")) {
        QDBusInterface notify("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
        notify.asyncCall(QString("Notify"),
                         QString("dde-file-manager"),   // title
                         static_cast<uint>(0),
                         QString("dde-file-manager"),   // icon
                         QObject::tr("This system wallpaper is locked. Please contact your admin."),
                         QString(), QStringList(), QVariantMap(), 5000);
        qInfo() << "wallpaper is locked..";
        return false;
    }

    QDBusMessage msgIntrospect = QDBusMessage::createMethodCall(APPEARANCE_SERVICE,
                                                                APPEARANCE_PATH,
                                                                "org.freedesktop.DBus.Introspectable",
                                                                "Introspect");
    QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(msgIntrospect);
    call.waitForFinished();
    if (call.isFinished()) {
        QDBusReply<QString> reply = call.reply();
        QString value = reply.value();

        if (value.contains("SetMonitorBackground")) {
            QDBusMessage msg = QDBusMessage::createMethodCall(APPEARANCE_SERVICE,
                                                              APPEARANCE_PATH,
                                                              APPEARANCE_SERVICE,
                                                              "SetMonitorBackground");
            // dde-desktop 中的代码是这样的
            // const QString screen = qApp->primaryScreen()->name();
            // 为了用程序所在屏幕就改为这样的
            const QString screen = qApp->allWindows().first()->screen()->name();
            msg.setArguments({ screen, pictureFilePath });
            QDBusConnection::sessionBus().asyncCall(msg);
            qInfo() << "setBackground calls Appearance SetMonitorBackground" << screen;
            return true;
        }
    }

    QDBusMessage msg = QDBusMessage::createMethodCall(APPEARANCE_SERVICE,
                                                      APPEARANCE_PATH,
                                                      APPEARANCE_SERVICE,
                                                      "Set");
    msg.setArguments({ "Background", pictureFilePath });
    QDBusConnection::sessionBus().asyncCall(msg);
    qInfo() << "setBackground calls Appearance Set";

    return true;
}
